﻿using System;
using System.Linq;
using System.Configuration;
using System.Data;

namespace ZJUploadTool
{
    public class UploadConfig
    {
        public string username
        {
            get { return ConfigurationManager.AppSettings["username"]; }
        }
        public string password
        {
            get { return ConfigurationManager.AppSettings["password"]; }
        }
        public string stationID
        {
            get { return ConfigurationManager.AppSettings["stationID"]; }
        }

        public string stationName
        {
            get { return ConfigurationManager.AppSettings["detectStationName"]; }
        }

        public string accessToken
        {
            get 
            {
                DataTable recordSetPtr = new DataTable();
                string sql = "SELECT TOP 1 * FROM [IVS30_debug].[dbo].[ZJ_UPLOAD_TOKEN] ORDER BY ID DESC";
                DBUtility.DbHelper.GetTable(sql, ref recordSetPtr);
                var row = (from r in recordSetPtr.Rows.Cast<DataRow>() select r).FirstOrDefault();
                return row.Field<string>("AccessToken");
            }
            set 
            {
                string sql = "INSERT INTO [IVS30_debug].[dbo].[ZJ_UPLOAD_TOKEN]"
                + "([AccessToken]"
                + ",[TIME])"
                + "VALUES"
                + string.Format("('{0}','{1}')", value, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                DBUtility.DbHelper.ExecuteSql(sql);
            }
        }
        public string url
        {
            get { return ConfigurationManager.AppSettings["url"]; }
        }
        public static UploadConfig GetConfig()
        {
            if (m_Instance == null)
            {
                m_Instance = new UploadConfig();
                return m_Instance;
            }
            else
                return m_Instance;
        }
        private static UploadConfig m_Instance;
        private UploadConfig() { }
    }
}
